<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        li {
            list-style: none;
        }
    </style>
</head>

<body>
    <input type="text">
    <ul></ul>
    <script>
        window.onload = function () {
            var inpElm = document.querySelector("input")
            var ulElm = document.querySelector("ul")
            var a = 0;
            inpElm.onkeyup = function (event) {
                if (event.keyCode === 13) {
                    var content = this.value;
                    if (content.trim()) {
                        var liElm = document.createElement("li");
                        liElm.innerHTML = `${inpElm.value}`;
                        a++;
                        // 后一个不可以不传，但是可以传空（null），传空效果相当于appendChild
                        ulElm.insertBefore(liElm, ulElm.children[0]);
                        if (a > 5) {  
                            ulElm.lastElementChild.remove()
                        }
                    } else {
                        alert("null")
                    }
                    inpElm.value = "";
                }
            }
        }

    </script>
</body>

</html>